(function () {
    var io = ppt.io;
    function ShopController() {
        ShopController.super(this);
        this.ASSETS = {
            RES: ASSETS.SHOPERES,
            TYPE: "simple"
        }
        this.BG = null;    //公共背景龙骨动画

        this.shoppage = null;
        this.shoptab = null;
        this.shoplist = null;
        this.tabIndex = 0;
        this.init();
    }
    Laya.class(ShopController, "Scene.Shop", Laya.Sprite);
    var _proto = ShopController.prototype;
    _proto.destroy = function () {
        Laya.Sprite.prototype.destroy.call(this, true);

        this.BG = null;    //公共背景龙骨动画
        this.tabIndex = null;
        this.shoppage = null;
        this.shoptab = null;
        this.shoplist = null;
    }
    _proto.init = function () {
        this.size(GAME_CONFIG.WIDTH, Laya.stage.height);
        io.register("shop::buy", this, this.buyGo);
        io.register("shop::shopInfo", this, this.changeData);
        io.register("pve::wait", this, this.pvehome);
        io.register("game::ready", this, this.gameReady);
    }
    _proto.createUI = function () {
        var BG = utils.createSkeleton('res/public/public_bg');
        BG.pos(GAME_CONFIG.WIDTH / 2, Laya.stage.height / 2);
        var shoppage = new Com.Shop.Page();
        shoppage.height = Laya.stage.height;
        shoppage.bottom = 30;
        shoppage.top = 120;
        shoppage.x = 350;
        this.shoppage = shoppage;
        var shopList = new Com.Shop.List();
        shopList.height = Laya.stage.height;
        shopList.bottom = 50;
        shopList.x = 380;
        this.shoplist = shopList;
        var shoptab = new Com.Shop.Tab(function (index) {
            this.tabIndex = index;
            this.shoppage.timeChange(index);
        }.bind(this));
        shoptab.pos(50, 170);
        this.BG = BG;
        BG.play('public_bg', true);
        this.addChildren(BG, shoppage, shoptab, shopList);
    }
    _proto.buyGo = function (data) {
        var config = {
            msg: data.msg,
            close: true,
            btn: {
                btnCancel: false
            }
        }

        switch (data.code) {
            case 200:
                ppt.director.popScene(new Alert.BuySuccess(data), { shadowAlpha: 0.7 });
                io.emit({ 'cmd': 'game::enter', params: {} });
                io.emit({ "cmd": "shop::shopInfo", params: { 'currency': this.tabIndex + 1 } });
                return;
            case 203:
                config.btn.btnConfirm = {
                    skin: "confirm"
                }
                break;
            case 205:
                config.btn = {
                    btnCancel: {
                        skin: "challenge",
                        click: function () {
                            io.emit({ cmd: 'pve::wait', params: { type: 6 } });
                        }
                    },
                    btnConfirm: {
                        skin: "race",
                        click: function () {
                            ppt.director.runScene(new Scene.Matching());
                        }
                    }
                }
                break;
            case 206:
                config.btn = {
                    btnCancel: {
                        skin: "cancel"
                    },
                    btnConfirm: {
                        skin: "confirm",
                        click: function () {
                            if(GM.appStorePay == 1 && (window.wltgame && wltgame.suportStorePay == 1) || GM.appStorePay == 2){
                                location.href = "/?act=ios_applepay&st=applepay_game_vertical&gameId=" + GM.gameId + "&redirect_uri=" + redirect_uri;
                            }else{
                                ppt.director.runScene(new Scene.Recharge);
                            }
                        }
                    }
                }
                break;
            case 207:
                config.btn.btnConfirm = {
                    skin: "race",
                    click: function () {
                        //io.emit({cmd:'pve::wait', params : {type : 6}});
                        ppt.director.runScene(new Scene.Matching());
                    }
                }
                break;
            default:
                config.btn.btnConfirm = {
                    skin: "confirm"
                }
        }
        ppt.director.popScene(new Alert.Public(config));
    }
    _proto.changeData = function (data) {
        this.data = data;
        this.shoplist.upData(data, this.tabIndex);
    }
    _proto.gameReady = function (data) {
        console.log("Shop Scene to GameScene");
        ppt.director.runScene(new Scene.Game(data));
    }
    _proto.pvehome = function (data) {
        var config = {
            msg: data.msg,
            close: true,
            btn: {
                btnCancel: false,
                btnConfirm: {
                    skin: "confirm"
                }
            }
        }
        switch (data.code) {
            case 103://pve次数已用完
            case 116://pvp无法进入pve
                ppt.director.popScene(new Alert.Public(config));
                break;
        }
    }
    _proto.onAdd = function () {
        this.createUI();

    }
    _proto.onEnter = function () {
        io.emit({ "cmd": "shop::shopInfo", params: { 'currency': 1 } });
        ppt.header.enter("shop");
    }
    _proto.onExit = function () {
        io.unregister("shop::shopInfo");
        io.unregister("shop::buy");
        io.unregister("pve::wait", this.pvehome);
        io.unregister("game::ready", this.gameReady);
        this.destroy(true);
    }
    _proto.onResize = function (height) {
        this.height = height;
        this.BG.y = height / 2;
        this.shoppage.bottom = 30;
        this.shoplist.height = height;
        this.shoplist.bottom = 50;
    }
})();
